//
//  MRegistService.m
//  Index
//
//  Created by Apple－12 on 15/10/27.
//  Copyright (c) 2015年 Apple－12. All rights reserved.
//

#import "MRegistService.h"
//注册
#define  REGISTER_URL [NSString stringWithFormat:@"%@%@",BASE_SERVER_URL,@"/api/User"]
//登陆
#define LOGIN_URL [NSString stringWithFormat:@"%@",@"/api/User"]




//  @"%@?username=%@&password=%@"
@implementation MRegistService
+(id)sharedClient
{
    static dispatch_once_t pred;
    static MRegistService *shared_instance=nil;
    dispatch_once(&pred, ^{
        shared_instance=[[self alloc] init];
        shared_instance.client=[self sharedHTTPClient];
    });
    return  shared_instance;
}
-(void)getUserWithUsername:(NSString *)username
                    password:(NSString *)password
                  completion:(getProductListCompletionHandler) success
                     failure:(getProductErrorHandler) failure{
    NSMutableDictionary *params = [[NSMutableDictionary alloc]init];
    if (username) {
        [params setObject:username forKey:@"username"];
    }if (password) {
        [params setObject:password forKey:@"password"];
    }else{
        [params setObject:@"0" forKey:@"password"];
    }
    
    
    NSString *path = REGISTER_URL;
    if (params.count > 0) {
        path = [NSString stringWithFormat:@"%@?",path];
        for (id key in params) {
        path = [NSString stringWithFormat:@"%@%@=%@&",path,key,params[key]];
        }
    }
    [self.client postPath:path parameters:nil success:^(AFHTTPRequestOperation *operation,id responseObject){
        if (operation.response.statusCode == 200) {
            
            success(@"注册成功！！！");
            
            
        }else{
            
            success(@"注册失败！！！");
            
            
        }
    } failure:^(AFHTTPRequestOperation *operation,NSError *error){
        failure(@"网络异常");
    }];

}

-(void)getLoginWithUsername:(NSString *)username
                   password:(NSString *)password
                 completion:(getLoginSystemCompletionHandler)success
                    failure:(getLoginErrorHandler) failure{
    NSMutableDictionary *params = [[NSMutableDictionary alloc]initWithObjectsAndKeys:username,@"username",password,@"password", nil];
    
    
    [self.client getPath:LOGIN_URL
              parameters:params
                 success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        id JSON = ((AFJSONRequestOperation*)operation).responseJSON;
        
        if ([JSON isKindOfClass:[NSDictionary class]]) {

            NSLog(@"succees");
            success([[MUserModel alloc]initWithJSON:JSON]);
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        failure(@"暂时无法访问，请稍后重试！");
    }];


}



@end
